package com.nidu.demo.menu.executor.query;

import com.alibaba.cola.dto.SingleResponse;
import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.menu.convertor.MenuConvertor;
import com.nidu.demo.menu.dto.MenuCO;
import com.nidu.demo.menu.gateway.MenuGateway;
import com.nidu.demo.menu.model.Menu;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * 菜单单查询执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class MenuSingleQryExe {

    private final MenuGateway gateway;

    private final MenuConvertor convertor;

    public SingleResponse<MenuCO> execute(Long id) {
        // 1. 查询数据
        Menu menu = gateway.getById(id);
        if(menu == null){
            throw new BizException(ErrorCodeConstants.MENU_NOT_FOUND.getCode(), ErrorCodeConstants.MENU_NOT_FOUND.getMessage());
        }
        // 2. 转换为DTO返回
        MenuCO menuCO = convertor.toClientObject(menu);

        return SingleResponse.of(menuCO);
    }

}